import React from 'react';
import { Input } from 'antd';
import TextArea from './TextArea';

const Index = (props) => {
  const {
    type = 'string', // string, number => 数据类型
    trim = true,
    title = true, // true, '' => 自定义字符串

    disabled,
    value,
    onChange,
    onBlur,
    ...rest
  } = props;

  if (!Object.keys(props).includes('value')) {
    return 'DEPRECATED';
  }

  if (disabled && disabled.render) {
    return disabled.render(value, { value }) || <span>&nbsp;</span>;
  }

  return (
    <Input
      {...rest}
      disabled={!!disabled}
      value={value}
      onChange={(e) => {
        if (onChange) {
          if (type.toLowerCase() === 'number' && /^-?\d+(\.\d*[1-9]+)?$/g.test(e.target.value)) {
            onChange(Number(e.target.value));
          } else {
            onChange(e.target.value);
          }
        }
      }}
      onBlur={(e) => {
        if (onChange && type.toLowerCase() === 'string' && trim && /(^\s)|(\s$)/.test(value)) {
          onChange(value?.trim());
        }
        if (onBlur) {
          onBlur(e);
        }
      }}
      title={typeof title === 'string' ? title : value}
    />
  );
};

Index.TextArea = TextArea;
Object.keys(Input).forEach((item) => {
  if (!['defaultProps', 'propTypes', 'TextArea'].includes(item)) {
    Index[item] = Input[item];
  }
});
Index.displayName = 'Input';
export default Index;
